﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsFavoriteTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// FavoriteList Business Object
    /// </summary>

    public class FavoriteList : BOBase
    {

        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected FavoriteList() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// UserID of the FavoriteList.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;

                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;

        /// <summary>
        /// FavoriteID.
        /// </summary>
        public int FavoriteID
        {
            get { return _favoriteID; }
            set
            {
                if (_favoriteID != value)
                {
                    if (OnPropertyChanging("FavoriteID", _favoriteID, value))
                    {
                        int oldValue = _favoriteID;
                        _favoriteID = value;
                        OnPropertyChanged("FavoriteID", oldValue, value);
                    }
                }
            }
        }  private int _favoriteID = -1;


        /// <summary>
        /// FavoriteName.
        /// </summary>
        public string FavoriteName
        {
            get { return _favoriteName; }
            set
            {
                if (_favoriteName != value)
                {
                    if (OnPropertyChanging("FavoriteName", _favoriteName, value))
                    {
                        string oldValue = _favoriteName;
                        _favoriteName = value;
                        OnPropertyChanged("FavoriteName", oldValue, value);
                    }
                }
            }
        }  private string _favoriteName = null;



        /// <summary>
        /// Remark.
        /// </summary>
        public string Remark
        {
            get { return _remark; }
            set
            {
                if (_remark != value)
                {
                    if (OnPropertyChanging("Remark", _remark, value))
                    {
                        string oldValue = _remark;
                        _remark = value;
                        OnPropertyChanged("Remark", oldValue, value);
                    }
                }
            }
        }  private string _remark = null;




        /// <summary>
        /// Date/time at which the Job was added to FavoriteList.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty FavoriteList object.
        /// </summary>
        public static FavoriteList CreateNew()
        {
            FavoriteList obj = new FavoriteList();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.AddedDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static FavoriteList CreateNew(FavoriteListCollection collection)
        {
            FavoriteList obj = CreateNew();

            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllFavoriteListByUserID

        /// <summary>
        /// Returns a collection of all FavoriteList.
        /// </summary>
        public static FavoriteListCollection GetAllFavoriteListByUserID(int userid)
        {
            FavoriteListTableAdapter adapter = new FavoriteListTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsFavorite.FavoriteListDataTable tbl = adapter.GetData(userid);

            FavoriteListCollection list = new FavoriteListCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsFavorite.FavoriteListRow row in tbl.Rows)
                {
                    FavoriteList favoriteList = new FavoriteList();
                    RelationalToObject(favoriteList, row);
                    list.Add(favoriteList);
                }
            }

            return (list);
        }

        #endregion

        #region GetFavoriteListByUserID_FavoriteID

        /// <summary>
        /// Returns a collection of all FavoriteList to check for duplicacy.
        /// </summary>
        public static FavoriteList GetFavoriteListByUserID_FavoriteID(int userid, int favoriteid)
        {
            FavoriteListTableAdapter adapter = new FavoriteListTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsFavorite.FavoriteListDataTable tbl = adapter.GetDataByUserIDnFavID(userid, favoriteid);

            if (tbl.Rows.Count == 1)
            {
                FavoriteList fl = new FavoriteList();
                RelationalToObject(fl, tbl.Rows[0]);
                return (fl);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'FavoriteID' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified FavoriteList.
        /// </summary>
        public static void Update(FavoriteList obj)
        {
            bool updateFavoriteList = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateFavoriteList = (obj.ObjState != ObjectState.Unchanged);

                if (!updateFavoriteList)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsFavorite.FavoriteListDataTable tbl = null;
            if (updateFavoriteList)
            {
                FavoriteListTableAdapter adapter = new FavoriteListTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsFavorite.FavoriteListDataTable();
                dsFavorite.FavoriteListRow row = tbl.NewFavoriteListRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("UserID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region Delete

        /// <summary>
        /// Delete a favorite list record.
        /// </summary>
        public static int Delete(int userid, int favoriteid)
        {
            FavoriteListTableAdapter adapter = new FavoriteListTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            int d = adapter.Delete(userid, favoriteid);

            return (d);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("FavoriteList: UserID={0}, FavoriteID={1}, {2}", UserID, FavoriteID, Remark));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more FavoriteList objects.
    /// </summary>

    public class FavoriteListCollection : List<FavoriteList>
    {
        #region Types

        public class UserComparer : IComparer<FavoriteList>
        {
            public int Compare(FavoriteList fl1, FavoriteList fl2)
            {
                return (String.Compare(fl1.UserID.ToString(), fl1.UserID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindByFavoriteListUserID

        public FavoriteList FindByFavoriteListUserID(int userID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].UserID == userID)
                    return (this[ndx]);

            return (null);
        }

        #endregion


        #endregion
    }
}
